{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from decimal import Decimal"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.2, 1.5, 1.8],\n",
       "       [1.3, 1.4, 1.9],\n",
       "       [1.1, 1.6, 1.7]])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([[1.2, 1.5, 1.8],\n",
    "              [1.3, 1.4, 1.9],\n",
    "              [1.1, 1.6, 1.7]])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 5, 10,  9])"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y = np.array([5, 10, 9]).T\n",
    "y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  1、使用循环的方式计算每天的采购总金额 得到结果为[37.2, 37.6, 36.8]，分别表示7/28、7/29、7/30这三天采购总额"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[Decimal('37.2'), Decimal('37.6'), Decimal('36.8')]"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def res1():\n",
    "    result = []\n",
    "    for i in range(3):\n",
    "        res = 0\n",
    "        for j in range(3):\n",
    "            rx = x[i,j]\n",
    "            ry = y[j]\n",
    "            res += Decimal(str(rx))*Decimal(str(ry))\n",
    "        result.append(res)\n",
    "    return result\n",
    "res1()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "37.2\n",
      "37.6\n",
      "36.8\n"
     ]
    }
   ],
   "source": [
    "for i in result:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、使用矩阵点乘来计算每天的采购总金额（使用np.dot来实现矩阵相乘）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([37.2, 37.6, 36.8])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "z = np.dot(x, y)\n",
    "z"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、测试两种方式的性能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%time cp1 = np.dot(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Wall time: 0 ns\n"
     ]
    }
   ],
   "source": [
    "%time cp2 = res1()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 作业2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([6, 9, 6, 1, 1, 2, 8, 7, 3, 5, 6, 3, 5, 3, 5, 8, 8, 2, 8, 1, 7, 8,\n",
       "       7, 2, 1, 2, 9, 9, 4, 9])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.random.seed(1)\n",
    "X = np.random.randint(1, 10, 30)\n",
    "X"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1、请将X处理为一个3列的矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9, 6],\n",
       "       [1, 1, 2],\n",
       "       [8, 7, 3],\n",
       "       [5, 6, 3],\n",
       "       [5, 3, 5],\n",
       "       [8, 8, 2],\n",
       "       [8, 1, 7],\n",
       "       [8, 7, 2],\n",
       "       [1, 2, 9],\n",
       "       [9, 4, 9]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y = X.reshape(10,3)\n",
    "Y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2、将第三列中，小于等于3的修改为0、大于3且小于等于6的修改为1、大于6的修改为2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9, 1],\n",
       "       [1, 1, 0],\n",
       "       [8, 7, 0],\n",
       "       [5, 6, 0],\n",
       "       [5, 3, 1],\n",
       "       [8, 8, 0],\n",
       "       [8, 1, 2],\n",
       "       [8, 7, 0],\n",
       "       [1, 2, 2],\n",
       "       [9, 4, 2]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for i in range(10):\n",
    "    if Y[i,2] <= 3:\n",
    "        Y[i,2] = 0\n",
    "    elif Y[i,2] > 6:\n",
    "        Y[i,2] = 2\n",
    "    else:\n",
    "        Y[i,2] = 1\n",
    "Y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3、假设这是10条样本数据，前两列是样本的两个特征，第3列是样本的分类标记，请分离出样本的特征和分类 标记，分别存放在两个变量中，用 X_train 存放样本特征(红色部份), y_train 存放分类标记(绿色部份)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9],\n",
       "       [1, 1],\n",
       "       [8, 7],\n",
       "       [5, 6],\n",
       "       [5, 3],\n",
       "       [8, 8],\n",
       "       [8, 1],\n",
       "       [8, 7],\n",
       "       [1, 2],\n",
       "       [9, 4]])"
      ]
     },
     "execution_count": 135,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_train = Y[:,:2]\n",
    "X_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [1],\n",
       "       [0],\n",
       "       [2],\n",
       "       [0],\n",
       "       [2],\n",
       "       [2]])"
      ]
     },
     "execution_count": 146,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_train = Y[:,2:3]\n",
    "y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 184,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 9 1]\n",
      "1\n",
      "[1 1 0]\n",
      "0\n",
      "[1 1 0]\n",
      "[8 7 0]\n",
      "0\n",
      "[8 7 0]\n",
      "[5 6 0]\n",
      "0\n",
      "[5 6 0]\n",
      "[5 3 1]\n",
      "1\n",
      "[8 8 0]\n",
      "0\n",
      "[8 8 0]\n",
      "[8 1 2]\n",
      "2\n",
      "[8 7 0]\n",
      "0\n",
      "[8 7 0]\n",
      "[1 2 2]\n",
      "2\n",
      "[9 4 2]\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "X_train0 = []\n",
    "X_train1 = []\n",
    "X_train2 = []\n",
    "x_0 = 0\n",
    "x_1 = 0\n",
    "x_2 = 0\n",
    "for flag in Y:\n",
    "    print(flag)\n",
    "    flag1 = flag[2]\n",
    "    print(flag1)\n",
    "    if flag1 == 0:\n",
    "        print(flag)\n",
    "        x_0 += 1\n",
    "        X_train0 = np.concatenate([X_train0,flag[0:2]])\n",
    "    elif flag1 == 1:\n",
    "        x_1 += 1\n",
    "        X_train1 = np.concatenate([X_train1,flag[0:2]])\n",
    "    else:\n",
    "        x_2 += 1\n",
    "        X_train2 = np.concatenate([X_train2,flag[0:2]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 1.],\n",
       "       [8., 7.],\n",
       "       [5., 6.],\n",
       "       [8., 8.],\n",
       "       [8., 7.]])"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分类为0的样本\n",
    "X0 = X_train0.reshape(x_0,2)\n",
    "X0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6., 9.],\n",
       "       [5., 3.]])"
      ]
     },
     "execution_count": 171,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分类为1的样本\n",
    "X_train1.reshape(x_1,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 172,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8., 1.],\n",
       "       [1., 2.],\n",
       "       [9., 4.]])"
      ]
     },
     "execution_count": 172,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分类为2的样本\n",
    "X_train2.reshape(x_2,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
